<?php

namespace addons\csmitsm\library\xcore\xcore\base;

use addons\csmitsm\library\xcore\xcore\base\XcABackend;
use addons\csmitsm\library\xcore\xcore\utils\XcDaoUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcRequestUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcResponseUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcTransactionUtils;
use addons\csmitsm\library\xcore\xcore\XcException;

/**
 * Backend的基础类(Save & Submit 版本)
 */
abstract class XcASubmitBackend extends XcABackend
{
    public function add()
    {
        XcResponseUtils::redirect("edit?dialog=1");
    }

    protected function beforeCreateOrUpdateSaveSubmit(&$params)
    {
        trace("beforeCreateOrUpdateSaveSubmit");
    }
    protected function beforeCreateOrUpdateSubmitSubmit(&$params)
    {
        trace("beforeCreateOrUpdateSubmitSubmit");
    }

    protected function afterCreateOrUpdateSaveSubmit($row)
    {
        trace("afterCreateOrUpdateSaveSubmit");
    }
    protected function afterCreateOrUpdateSubmitSubmit($row)
    {
        trace("afterCreateOrUpdateSubmitSubmit");
    }

    private function createOrUpdateSubmit()
    {
        $id = XcRequestUtils::param(["ids", "id", "xc_tabid"]);
        $params = $this->request->post('row/a');
        $params = $this->preExcludeFields($params);

        // 如xc_action存在: save则draft,submit则normal
        $xc_action = XcRequestUtils::post("xc_action");
        $params['status'] = ($xc_action == 'submit') ? 'normal' : 'draft';

        $row = XcTransactionUtils::service(function () use ($id, $xc_action, $params) {

            if ($xc_action == 'submit') {
                $this->beforeCreateOrUpdateSubmitSubmit($params);
            } else {
                $this->beforeCreateOrUpdateSaveSubmit($params);
            }

            $result = null;
            if (empty($id)) {
                $result = $this->model->allowField(true)->save($params);
                $id = $this->model->id;
            } else {
                $row = $this->model->get($id);
                $result = $row->allowField(true)->save($params);
            }

            if ($result === false) {
                throw new XcException(__('No rows were inserted'));
            }

            $row = XcDaoUtils::getRowById($this->model, $id);
            if ($xc_action == 'submit') {
                $this->afterCreateOrUpdateSubmitSubmit($row);
            } else {
                $this->afterCreateOrUpdateSaveSubmit($row);
            }
            
            return $row;
        });

        return $row;
    }

    public function edit($ids = null)
    {
        $id = XcRequestUtils::param(["ids", "id", "xc_tabid"]);
        $row = empty($id) ? ["nextruntime" => null, "status" => "draft"] : XcDaoUtils::getRowById($this->model, $id);

        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);
            return $this->view->fetch();
        } else {
            $row = $this->createOrUpdateSubmit();
            $this->success('', null, ['operate' => empty($id) ? 'create' : 'update', 'row' => $row]);
        }
    }
}
